(function($, window, undefined) {
    $.EtDeQrcode = $.EtDeQrcode || {};
	$.EtDeQrcode.list = {};
	
	$.extend($.EtDeQrcode, {
        "version": "1.0.0",
		//定义外部扩展方法接口
        "extend": function(methods) {
            $.extend($.fn.EtDeQrcode, methods);
			
			if (!this.no_legacy_api) {
				$.fn.extend(methods);
			}
        }
    });
	
	//常用扩展方法
	$.EtDeQrcode.extend({
    });
	
	
	$.fn.EtDeQrcode = function(options) {
        var _this = this;

		//合并参数
        var config = $.extend({},$.fn.EtDeQrcode.defaults, options);
        
		//内部扩展
		var bosom = {
		};

		_this.bosom = bosom;
		_this.config = config;
		
		//初始化
		init(_this);

		$.EtDeQrcode.list[config.gridId] = _this;
		
		return _this;
    };
	
	$.fn.EtDeQrcode.defaults = {
		deqrId: '', //解码Flash对象的Id
		swfPath: '', //二维码解码工具路径
		swfObj: null, //解码工具实例化后的Flash对象
		result: '' //解码结果
    };
	
	/**************************私有方法开始***************************/
	//初始化方法
	function init(_this){
		var _that = _this;
		var html = '';
		html += '<div class="swf_header">';
		html += '</div>';
		html += '<div class="swf_wraper">';
		html += createSwfObj(_that);
		html += '</div>';
		html += '<div class="swf_result">';
		html += '</div>';
		html += '<div class="swf_toolbar">';
		html += '	<input type="hidden" id="decode_result" value=""/>';
		html += '	<input type="button" id="decode_reload" value="重新扫描"/>';
		html += '	<input type="button" id="decode_close" value="关闭扫描"/>';
		html += '</div>';
		
		_that.html(html);
		
		_that.config.swfObj = _that.find('#'+_that.config.deqrId)[0];
		
		_that.find('#decode_result').live('change',function(){
			_that.config.result = $(this).val();
			_that.find('.swf_result').html($(this).val());
			//console.log(_that);
		});	
	}
	
	//浏览器是否支持Flash
	function hasFlashPlugin(){
		if(!!navigator.userAgent.match(/MSIE/)){
			try{
				new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				return true;
			}catch(ex){
				return false;
			}
		}else{
			var swf=navigator.plugins["Shockwave Flash"];
			if(swf){
				return true;
			}else{
				return false;
			}
		}
	}
	
	//创建二维码扫描SWF的Flash对象
	function createSwfObj(_this) {
		var _that = _this;		
		var html = '';
		if (hasFlashPlugin()) {
			if ( !! navigator.userAgent.match(/MSIE/)) { //ie浏览器
				html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="600" height="450" id="'+_that.config.deqrId+'" align="middle">';
				html += '<param name="allowScriptAccess" value="always" />';
				html += '<param name="wmode" value="transparent">';
				html += '<param name="allowFullScreen" value="false" />';
				html += '<param name="movie" value="'+_that.config.swfPath+'" />';
				html += '<param name="loop" value="false" />';
				html += '<param name="menu" value="false" />';
				html += '<param name="quality" value="best" />';
				html += '<param name="bgcolor" value="#ffffff" />';
				html += '<param name="flashvars" value=""/>';
				html += '</object>';
			} else {
				html += '<embed id="'+_that.config.deqrId+'" src="'+_that.config.swfPath+'" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="600" height="450" name="webcam_movie" align="middle" wmode="transparent" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="" />';
			}
		} else {
			var pageHost = ((document.location.protocol == "https:") ? "https://" : "http://");
			html += "<a href='http://www.adobe.com/go/getflashplayer'><img src='" + pageHost + "www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a></br><p>您的浏览器未安装flash插件或者已被禁用，请点击图片下载该插件，或者启用您的flash插件</p>";
	
		}
		
		return html;
	}
	
	//移除扫描工具
	function removeSwfObj(_this) {
		var _that = _this;
		_that.find('.swf_wraper').empty();
	}
	
	
	
	//window.JsHello = JsHello;
})(jQuery, window);
//获取扫描结果
function JsHello(msg){
	//console.log(msg);
	$('#decode_result').val(msg);
	$('#decode_result').change();
}